package com.samphanie.auiu.common.http

import com.alibaba.android.arouter.launcher.ARouter
import com.samphanie.auiu.library.restful.AuiuInterceptor
import com.samphanie.auiu.library.restful.AuiuResponse
import org.json.JSONObject

/**
 * 根据response 的 code 自动路由到相关页面
 * @author Dries
 **/
class HttpStatusInterceptor : AuiuInterceptor {
    override fun intercept(chain: AuiuInterceptor.Chain): Boolean {
        val response = chain.response()
        if (!chain.isRequestPeriod && response != null) {
            when (response.code) {
                AuiuResponse.RC_NEED_LOGIN, AuiuResponse.RC_AUTH_TOKEN_EXPIRED -> {
                    ARouter.getInstance().build("/account/login").navigation()
                }
                AuiuResponse.RC_ACCOUNT_INVALID, AuiuResponse.RC_USER_FORBID -> {
                    var helpUrl: String? = null
                    if (response.data != null) {
                        helpUrl = response.data!!.toString()
                    }
                    ARouter.getInstance().build("/degrade/global/activity")
                        .withString("degrade_title", "非法访问")
                        .withString("degrade_desc", response.message)
                        .withString("degrade_action", helpUrl)
                        .navigation()
                }
            }
        }

        return false
    }
}